/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2018-12-13 Time: 22:23
 *
 * 我的工单
 */

import React, {Component} from 'react';
import {
    SafeAreaView,
    View,
    Text,
    TouchableOpacity,
    ScrollView,
} from 'react-native';
import {connect} from 'react-redux';
import {COLOR_THEME, COLOR_WHITE, FONT_COLOR_SUB_TITLE} from "../../../constant/PHColors"
import {styles} from './addCheckUnusualResultScreen.Style'
import {FetchStatus} from "../../../configuration/RequestConfiguration";
import {ReturnStatus} from "../../../configuration/ReturnStatus";
import FontAwesome from "react-native-vector-icons/FontAwesome";
import AntDesign from "react-native-vector-icons/AntDesign";
import {SCREEN_HEIGHT, SCREEN_WIDTH} from "../../../constant/PHScreen";
import {Header} from "react-navigation";

class addCheckUnusualResultScreen extends Component {

    static navigationOptions = ({navigation}) => ({
        headerTitle: `上报查房结果`,
        headerRight: (
            <TouchableOpacity activeOpacity={0.9} onPress={navigation.getParam('addClick')}>
                <View style={{width: 80, height: 44, flexDirection: 'row', justifyContent: 'center', alignItems: 'center'}}>
                    <AntDesign name={'plus'} size={25} style={{color: '#212121', marginLeft: 10,fontWeight: 'bold'}}/>
                </View>
            </TouchableOpacity>
        )
    });

    // 构造
    constructor(props) {
        super(props);

        // 初始状态
        this.state = {
            place: this.props.navigation.getParam("place"),
            orderid: this.props.navigation.getParam("orderid"),
            orderpid: this.props.navigation.getParam("orderpid"),
            gno: this.props.navigation.getParam("gno"),
            resultJson:this.props.navigation.getParam("resultJson"),
        };

        this.floor_c = [];

    }

    componentWillMount() {
        this.props.navigation.setParams({addClick: this.add_button});
    }


    render() {
        return (
            <SafeAreaView style={styles.body}>

                <View style={[styles.body, {paddingBottom: 50}]}>
                    <ScrollView style={styles.scrollView}>
                        {
                            Object.keys(this.state.resultJson).length === 0
                                ?
                                (
                                    <TouchableOpacity activeOpacity={0.8} onPress={() => {
                                        this.props.navigation.navigate('addCheckUnusualScreen',{place:this.state.place,orderpid:this.state.orderpid,gno:this.state.gno,callBack:()=>this.freshScreen()});
                                    }}>
                                        <View style={{marginTop:10,marginRight: 15 ,flexDirection: 'column',alignItems: 'center',justifyContent: 'center',height:SCREEN_HEIGHT-Header.HEIGHT-40}}>
                                            <Text style={styles.text}>
                                                当前暂无查房上报记录,点击新建
                                            </Text>
                                        </View>
                                    </TouchableOpacity>
                                )
                                :
                                (
                                    this._render_result()
                                )
                        }

                    </ScrollView>
                </View>

                <View style={styles.bodyBottom}>

                    <TouchableOpacity activeOpacity={0.8} onPress={() => this.add_button()}>
                        <View style={styles.bodyBottomRight}>
                            <Text style={styles.cellTitleText}>
                                点击上报查房结果
                            </Text>
                        </View>
                    </TouchableOpacity>

                </View>

            </SafeAreaView>
        )
    }

    _render_result=()=>{
        let outHtml = []
        for (let val in this.state.resultJson){
            let value = this.state.resultJson[val];
            let html = (
                <View style={[styles.container,{marginTop:20}]}>
                    <View style={styles.orderTop}>
                        <View style={styles.iconBox}>
                            <AntDesign name={'clockcircleo'} size={20} style={{color: '#6ba1f9'}}/>
                        </View>
                        <View style={styles.textBox}>
                            <View style={styles.leftBox}>
                                <Text style={[styles.titleText,{marginLeft:5}]}>
                                    {value[0].crdate}
                                </Text>
                                <Text style={[styles.text,{color:'#6b6b6b',marginLeft:2,marginTop:3}]}>
                                    （GNO：{value[0].gno}）
                                </Text>
                            </View>
                        </View>
                        <View style={styles.arrowBox}>
                            <Text style={[styles.text,{marginTop:3}]}>
                                {value[0].log_user}
                            </Text>
                        </View>
                    </View>
                    <View style={styles.orderCenter}>
                        {
                            this.get_info('消费项目','消费',value)
                        }
                        {
                            this.get_info('遗留','遗留',value)
                        }
                        {
                            this.get_info('客房状态','备注',value)
                        }
                    </View>
                </View>
            );
            outHtml.push(html)
        }
        return outHtml

    };

    //下一步
    add_button=()=>{
        this.props.navigation.navigate('addCheckUnusualScreen',{place:this.state.place,orderpid:this.state.orderpid,gno:this.state.gno,callBack:()=>this.freshScreen()});
    };

    freshScreen=()=>{
        let {dispatch} = this.props;
        this.props.navigation.state.params.callBack();
    };

    get_info=(type,name,datesource)=>{
        let outHtml;
        let detailHtml = this.get_info_detail(type,datesource);
        if(detailHtml.length>0){
            outHtml = (
                <View style={styles.orderCenterLine}>
                    <View style={styles.orderCenterLeft}>
                        <Text style={styles.orderCenterText}>
                            {name}：
                        </Text>
                    </View>
                    <View style={styles.orderCenterRight}>
                        {
                            detailHtml
                        }
                    </View>
                </View>
            )
        }else{
            outHtml = (
                <View>

                </View>
            );
        }
        return outHtml
    }

    get_info_detail=(type,datesource)=>{
        let htmlArr=[];
        datesource.map((item, index) => {
            if(type===item.codetype){
                let html;
                html = (
                    <View style={{width:SCREEN_WIDTH * 2/3-30}}>
                        <Text style={styles.orderCenterText}>
                            {item.info_detail}
                        </Text>
                    </View>
                )
                htmlArr.push(html)
            }
        })
        return htmlArr
    }

}

const props = state => {
    return {

    }
}

export default addCheckUnusualResultScreen = connect(props)(addCheckUnusualResultScreen);